<template>
  <div>
    <el-button type="primary" @click="openAddDialog">添加学业成绩</el-button>

    <el-table :data="scores" style="margin-top: 10px" border>
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="courseName" label="课程名称" />
      <el-table-column prop="score" label="成绩" width="100" />
      <el-table-column label="是否为学位课" width="120">
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.isDegreeCourse">学位课</el-tag>
          <el-tag type="info" v-else>非学位课</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" title="学业成绩" @close="resetForm">
      <el-form :model="formData" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="formData.courseName" />
        </el-form-item>
        <el-form-item label="成绩" prop="score">
          <el-input-number v-model="formData.score" :min="0" :max="100" />
        </el-form-item>
        <el-form-item label="是否为学位课" prop="isDegreeCourse">
          <el-radio-group v-model="formData.isDegreeCourse">
            <el-radio :label="true">学位课</el-radio>
            <el-radio :label="false">非学位课</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'AcademicScoresModule',
  props: {
    studentId: { type: Number, required: true }
  },
  data() {
    return {
      scores: [],
      dialogVisible: false,
      formData: {
        id: null,
        studentId: null,
        courseName: '',
        score: null,
        isDegreeCourse: true
      },
      rules: {
        courseName: [{ required: true, message: '请输入课程名称', trigger: 'blur' }],
        score: [{ required: true, message: '请输入成绩', trigger: 'blur' }],
        isDegreeCourse: [{ required: true, message: '请选择是否为学位课', trigger: 'change' }]
      }
    }
  },
  watch: {
    studentId: {
      immediate: true,
      handler(newVal) {
        if (typeof newVal === 'number' && !isNaN(newVal)) {
          this.loadData()
        } else {
          this.scores = []
        }
      }
    }
  },
  methods: {
    async loadData() {
      try {
        const res = await axios.get(`/api/academicscores/student/${this.studentId}`)
        this.scores = res.data || []
      } catch (e) {
        this.$message.error('加载学业成绩失败')
      }
    },
    openAddDialog() {
      this.formData = {
        id: null,
        studentId: this.studentId,
        courseName: '',
        score: null,
        isDegreeCourse: true
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.formData = { ...row, studentId: this.studentId }
      this.dialogVisible = true
    },
    async submit() {
      try {
        const valid = await this.$refs.formRef.validate()
        if (!valid) return

        let res
        if (this.formData.id) {
          res = await axios.put('/api/academicscores', this.formData)
        } else {
          res = await axios.post('/api/academicscores', this.formData)
        }

        if (res.data.code === 200 || res.data > 0) {
          this.$message.success(this.formData.id ? '更新成功' : '添加成功')
          this.dialogVisible = false
          await this.loadData()
          this.$emit('save-success')
        } else {
          this.$message.error(res.data.message || (this.formData.id ? '更新失败' : '添加失败'))
        }
      } catch (e) {
        this.$message.error('提交失败')
      }
    },
    async handleDelete(id) {
      try {
        await axios.delete(`/api/academicscores/${id}`)
        this.$message.success('删除成功')
        await this.loadData()
        this.$emit('save-success')
      } catch (e) {
        this.$message.error('删除失败')
      }
    },
    resetForm() {
      this.$refs.formRef?.resetFields()
      this.formData = {
        id: null,
        studentId: this.studentId,
        courseName: '',
        score: null,
        isDegreeCourse: true
      }
    }
  }
}
</script>
